// pages/order/orderDetail/priceDetail/proceDetail.js
import { accAdd, accSub } from '../../../../sdk/utils/operation';
import { API_UPDATE_ORDER } from '../../../../sdk/business/url';
import getIn from "../../../../sdk/utils/getIn";
import {showFail, showSuccess} from "../../../../utils/util";
import {notify} from "../../../../sdk/utils/NotifyRegistryCenter";
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const link_products = JSON.parse(options.link_products);
    const id = options.id || null;
    let orderList = link_products.slice(1);
    const paid_price = Number(options.paid_price) || 0;
    const floor_price = Number(options.floor_price) || 0;
    const car_price = link_products[0].price || 0;
    const order_type = Number(options.order_type) || 0;
    const worktime = options.worktime;
    orderList && orderList.map(x => {
      x.attr_val = x.attr_val ? x.attr_val.split('：')[1] : ''
      return x;
    });
    this.setData({ orderList, paid_price, floor_price, car_price, totalPrice: paid_price, id, order_type, worktime })
  },
  changePrice(e) {
    let value = e.detail.value;
    const type = e.currentTarget.dataset.type;
    const reg = new RegExp('^(\\d+\\.\\d{2}).+');
    if (reg.test(value)) {
        value = this.data[type];
    }
    const totalPrice = accAdd(accSub(this.data.totalPrice, this.data[type]), value)
    this.setData({ [type]: value, totalPrice })
  },
    submit() {
        const token = getIn(getApp(), ['globalData', 'token']);
        const {id, car_price, floor_price} = this.data;
        global.request(API_UPDATE_ORDER, {token, id, car_price, floor_price}, (code, res) => {
          if(code == 'SUCCESS') {
              showSuccess('操作成功');
              notify('updateorderlist');
              notify('updateorderdetail');
              global.navigateHelper.navigateBack()
          } else if(code == 'FAIL') {
              showFail(res.msg);
          }

        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})